package com.Register.controller.admin;

import com.Register.constant.JwtClaimsConstant;
import com.Register.dto.*;
import com.Register.entity.PageQuery;
import com.Register.entity.User;
import com.Register.properties.JwtProperties;
import com.Register.result.PageResult;
import com.Register.result.Result;
import com.Register.service.AdminService;
import com.Register.utils.JwtUtil;
import com.Register.vo.UserWebLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name="管理员端接口")
@RestController("WebAdminController")
@RequestMapping("/web/admin")
@Slf4j
public class AdminController {

    @Resource
    private AdminService adminService;


    @Resource
    private JwtProperties jwtProperties;

    /**
     * 网页端管理员登录
     * @param userWebLoginDTO
     * @return
     */
    @Operation(summary="网页端管理员使用账号密码登录")
    @PostMapping("/login")
    public Result<UserWebLoginVO> login(@RequestBody UserWebLoginDTO userWebLoginDTO){
        log.info("web端-用户登录，参数：{}", userWebLoginDTO);

        // 用户登录逻辑
        //1、调用service方法
        User user=adminService.webAdminLogin(userWebLoginDTO);

        //2、登录成功后返回jwt令牌
        Map<String,Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.WEB_ADMIN_ID,user.getId());
        //生成token，注入相关jwtProperties配置属性
        String token= JwtUtil.createJWT(
                jwtProperties.getWebAdminSecretKey(),
                jwtProperties.getWebAdminTtl(),
                claims);

        //3、封装返回结果
        UserWebLoginVO userWebLoginVO = UserWebLoginVO.builder()
                .id(user.getId())
                .name(user.getName())
                .userName(user.getUsername())
                .token(token)
                .build();

        //4、返回结果
        return Result.success(userWebLoginVO);
    }

    /**
     * 管理员修改密码
     * @param adminUpdatePasswordDTO
     * @return
     */
    @PutMapping("/updatePassword")
    @Operation(summary = "管理员修改密码")
    public Result<String> UpdatePassword(@RequestBody AdminUpdatePasswordDTO adminUpdatePasswordDTO){
        boolean result=adminService.changePasswordByPhone(adminUpdatePasswordDTO);
        return result?Result.success("修改成功"):Result.error("修改失败");
    }

    @Operation(summary = "管理员启用账号")
    @PutMapping("/activeUser/{id}")
    public Result<String> setUserActive(@PathVariable("id") Long userId){
        adminService.setUserActive(userId);
        return Result.success("启用成功");
    }

    @PutMapping("/banUser/{id}")
    @Operation(summary = "管理员禁用账号")
    public Result<String> SetUserBan(@PathVariable("id") Long id){
        adminService.setUserBan(id);
        return Result.success("禁用成功");
    }

    @GetMapping("/getAll")
    @Operation(summary = "管理员分页查询全部用户")
    public Result<PageResult> getAllWxUser(@ParameterObject WxUserPageQueryDTO wxUserPageQueryDTO){
        return Result.success(adminService.getAllWxUser(wxUserPageQueryDTO));
    }

    @GetMapping("/getAllDeleted")
    @Operation(summary = "管理员分页查询全部禁用用户")
    public Result<PageResult> getAllDeletedWxUser(@ParameterObject PageQuery pageQuery){
        return Result.success(adminService.selectWxUserIsDeleted(pageQuery));
    }


    @PutMapping("/updateAdmin")
    @Operation(summary = "管理员更新自身信息")
    public Result<String> updateAdmin(@RequestBody AdminUpdateDTO adminUpdateDTO){
        return adminService.AdminUpdate(adminUpdateDTO);
    }

    /**
     * 用户退出
     */
    @PostMapping("/logout")
    @Operation(summary = "用户退出")
    public Result<String> logout() {
        log.info("用户退出");
        return Result.success();
    }
}
